<template>
    <!-- <StarTask :ref="el => myRefs['star']=el" v-if="user.mode===1" :list="list" @changeTab="changeTab">
      <template v-if="list.length>0">
          <nut-divider v-if="listStatus=='loading'">加载中...</nut-divider>
          <nut-divider v-else-if="listStatus=='nomore'">没有更多了</nut-divider>
      </template>
      <nut-empty description="暂无数据" v-else></nut-empty>
    </StarTask>
    <TalentTask :ref="el => myRefs['talent']=el" v-else :list="list" @changeTab="changeTab">
      <template v-if="list.length>0">
          <nut-divider v-if="listStatus=='loading'">加载中...</nut-divider>
          <nut-divider v-else-if="listStatus=='nomore'">没有更多了</nut-divider>
      </template>
      <nut-empty description="暂无数据" v-else></nut-empty>
    </TalentTask> -->
<view class="page task">
  <nut-tabs v-model="order.activetab"  title-scroll title-gutter="5" :auto-height="true" @change="changeTab" :name="String(order.activetab)">
      <nut-tabpane :title="item.name" v-for="item in tabs" :key="item.id" :pane-key="item.id">
          <view v-if="order.activetab==item.id">
              <template v-for="item in list">
                <TaskCardStar v-if="user.mode===1" :key="item.id" :item="item.activity" @click="goToSub('task/detail', item)"></TaskCardStar>
                <TaskCard v-else :key="user.mode+item.id" :item="item.activity" @click="goToSub('task/detail', item)" :type="1"></TaskCard>
              </template>
              <nut-empty image="error" description="您还未登录，任务需要登录后查看" v-if="!user.isLogin">
                  <div style="margin-top: 10px">
                      <nut-button type="info" @click="goToLogin">请登录</nut-button>
                  </div>
              </nut-empty>
              <template v-else-if="list.length>0">
                  <nut-divider v-if="listStatus=='loading'">加载中...</nut-divider>
                  <nut-divider v-else-if="listStatus=='nomore'">没有更多了</nut-divider>
              </template>
              <nut-empty description="暂无数据" v-else></nut-empty>
          </view>
      </nut-tabpane>
  </nut-tabs>
</view>
</template>

<script setup>
import TaskCard from '@/components/TaskCard'
import TaskCardStar from "@/components/TaskCardStar";
import { useReachBottom, useDidShow } from '@tarojs/taro'
import { ref } from 'vue'
import { useUserStore, useOrderStore } from "@/stores";
import { userTask } from '@/api/index'
import { navigateTo } from '@/router'
import "./index.scss";

definePageConfig({
  navigationBarTitleText: '全部任务'
})
// const StarTask = defineAsyncComponent(() => import('@/components/Task/Star'))
// const TalentTask = defineAsyncComponent(() => import('@/components/Task/talent'))
// const myRefs = reactive({})
const user = useUserStore()
const order = useOrderStore()
const listStatus = ref('loadmore')
const list = ref([])
const tabs = [
    {id: 0, name: '等待中'},
    {id: 1, name: '进行中'},
    {id: 5, name: '审核中'},
    {id: 2, name: '已完成'},
    {id: 6, name: '已驳回'},
    {id: 3, name: '已失效'},
    {id: 4, name: '已取消'},
    {id: 7, name: '报名未通过'},
]
let paging = { page: 1, size: 20, status: order.activetab }
let last_page = 0

function queryList(){
    if(!user.isLogin) return
    userTask(paging).then(res=>{
        if(res.meta){
            last_page = res.meta.last_page
        }
        if(res.data){
            list.value = list.value.concat(res.data)
        }
    })
}
const goToLogin = () => {
  navigateTo({ url: "/pages/login/index" });
}
const goToSub = (url, info) => {
  order.setTaskInfo(info)
  navigateTo({ url: "/pagesub/"+url });
}

useDidShow(()=>{
  if(!user.isLogin) return
  paging.status = order.activetab
  list.value = []
  queryList()
})

useReachBottom(()=>{
  console.log('触发底部')
  // const str = (user.mode===1 ? 'star' : 'talent')
  if(paging.page >= last_page) return listStatus.value = 'nomore';
  listStatus.value = 'loading'
  if(paging.page < last_page){
      paging.page += 1;
      queryList()
  }
})

function changeTab(tab){
  const id = Number(tab.paneKey)
  // tab切换重新请求数据
  if(paging.status==id) return
  order.setActiveTab(id)
  paging.status = id
  list.value = []
  queryList()
}
</script>
